/*
 * Software for Interactive Project Pool Game
 * Columbia University. New York, 2008
 * Authors:
 *   Abdulhamid Ghandour
 *   Thomas John
 *   Jaime Peretzman
 *   Bharadwaj Vellore
 * 
 * Desc:
 */
#ifndef I2C_H_
#define I2C_H_

/*
 * Start Bit
 */
#define START           \
    SDAT_SET;           \
    HALF_CLOCK_DELAY;   \
    SCLK_SET;           \
    HALF_CLOCK_DELAY;   \
    SDAT_CLR;           \
    HALF_CLOCK_DELAY;   \
    SCLK_CLR;           \
    HALF_CLOCK_DELAY;

/*
 * Stop Bit
 */
#define STOP            \
    SDAT_CLR;           \
    HALF_CLOCK_DELAY;   \
    SCLK_SET;           \
    HALF_CLOCK_DELAY;   \
    SDAT_SET;

/*
 * Sequence for a '1' bit
 */
#define SEND_BIT_1  \
  SDAT_SET;         \
  HALF_CLOCK_DELAY; \
  SCLK_SET;         \
  ONE_CLOCK_DELAY;  \
  SCLK_CLR;         \
  HALF_CLOCK_DELAY;\
  //SDAT_CLR;

/*
 * Sequence for a '0' bit
 */
#define SEND_BIT_0  \
  SDAT_CLR;         \
  HALF_CLOCK_DELAY; \
  SCLK_SET;         \
  ONE_CLOCK_DELAY;  \
  SCLK_CLR;         \
  HALF_CLOCK_DELAY;\
  //SDAT_SET;

#define SEND_0  \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_0;

#define SEND_1  \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_1;

#define SEND_2  \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_0;

#define SEND_3  \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_1;

#define SEND_4  \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_0;

#define SEND_5  \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_1;

#define SEND_6  \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_0;

#define SEND_7  \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_1;

#define SEND_8  \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_0;

#define SEND_9  \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_0; \
    SEND_BIT_1;

#define SEND_A  \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_0;

#define SEND_B  \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_1; \
    SEND_BIT_1;

#define SEND_C  \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_0;

#define SEND_D  \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_0; \
    SEND_BIT_1;

#define SEND_E  \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_0;

#define SEND_F  \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_1; \
    SEND_BIT_1;

#define READ(ack)       \
    SDAT_TRISTATE;      \
    HALF_CLOCK_DELAY;   \
    SCLK_SET;           \
    HALF_CLOCK_DELAY;   \
    ack = RD_ACK;       \
    HALF_CLOCK_DELAY;   \
    SCLK_CLR;           \
    HALF_CLOCK_DELAY;
    
#define ACK             \
    SDAT_CLR;           \
    HALF_CLOCK_DELAY;   \
    SCLK_SET;           \
    ONE_CLOCK_DELAY;    \
    SCLK_CLR;           \
    HALF_CLOCK_DELAY;

#define NACK            \
    SDAT_SET;           \
    HALF_CLOCK_DELAY;   \
    SCLK_SET;           \
    ONE_CLOCK_DELAY;    \
    SCLK_CLR;           \
    HALF_CLOCK_DELAY;   \
    SDAT_CLR;

#define COMM_INIT       \
    START;              \
    SEND_B;             \
    SEND_A;

#define READ_ACK(ack)    READ(ack)

int configureCamera();

#endif /*I2C_H_*/
